<?php
class FckModel extends CommonModel {
    //数据库名称

   public function xiangJiao($Pid=0,$DanShu=1){
        //========================================== 往上统计单数
        $where = array();
        $where['id'] = $Pid;
        $field = 'treeplace,father_id';
        $vo = $this ->where($where)->field($field)->find();
        if ($vo){
            $Fid = $vo['father_id'];
            $TPe = $vo['treeplace'];
            $table = $this->tablePrefix.'fck';
            if ($TPe == 0 && $Fid > 0){
                $this->execute("update ". $table ." Set `l`=l+$DanShu, `shangqi_l`=shangqi_l+$DanShu  where `id`=".$Fid);
            }elseif($TPe == 1 && $Fid > 0){
                $this->execute("update ". $table ." Set `r`=r+$DanShu, `shangqi_r`=shangqi_r+$DanShu  where `id`=".$Fid);
            }elseif($TPe == 2 && $Fid > 0){
                $this->execute("update ". $table ." Set `lr`=lr+$DanShu, `shangqi_lr`=shangqi_lr+$DanShu  where `id`=".$Fid);
            }
            if ($Fid > 0) $this->xiangJiao($Fid,$DanShu);
        }
        unset($where,$field,$vo);
    }
    
    
    


    public function addencAdd($ID=0,$inUserID=0,$money=0,$name=null,$UID=0,$time=0,$acttime=0,$bz=""){

        //添加 到数据表
        if ($UID > 0) {
            $where = array();
            $where['id'] = $UID;
            $frs = $this->where($where)->field('nickname')->find();
            $name_two = $name;
            $name = $frs['nickname'] . ' 开通会员 ' . $inUserID ;
            $inUserID = $frs['nickname'];
        }else{
            $name_two = $name;
        }

        $data = array();
        $history = M ('history');

        $data['user_id']        = $inUserID;
        $data['uid']            = $ID;
        $data['action_type']    = $name;
        if($time >0){
            $data['pdt']        = $time;
        }else{
            $data['pdt']        = mktime();
        }
        $data['epoints']        = $money;
        if(!empty($bz)){
            $data['bz']         = $bz;
        }else{
            $data['bz']         = $name;
        }
        $data['did']            = 0;
        $data['type']           = 1;
        $data['allp']           = 0;
        if($acttime>0){
            $data['act_pdt']    = $acttime;
        }
        $result = $history ->add($data);
        unset($data,$history);
    }

    /*
    *销售奖
    *
    *$ID            会员ID
    *$inUserID      获奖会员编号
    *s21            销售奖比例
    *$money         注册金额
    *$money_count   最后得到金额
    *$myid          推荐人id
    *$myusid        推荐人编号
    */
    public function xiaoshou($ID=0,$inUserID=0,$money=0){
        return false;
        $fee = M('fee');
        $fck = M('fck');
        $fee_rs = $fee->field('s21')->find(1);
        $s21=explode('|', $fee_rs['s21']);
        $where = array();
        $where['id'] = $ID;                 //re_id
        $where['is_fenh'] = array('eq',0);
        $field = 'id,user_id,u_level,cpzj,is_fx,is_hehuo';
        $frs = $fck->where($where)->field($field)->find();
        if ($frs){

            if($frs['is_fx'] != 0){
                $myid = $frs['id'];
                $myusid = $frs['user_id'];
                $uLevel = $frs['u_level'];
                $sss = $uLevel-1;

                $prii = $s21[$sss]/100;
                $money_count = bcmul($prii,$money,2);

                if($money_count>0){
                    $this->rw_bonus($myid,$inUserID,13,$money_count,0,'销售奖');
                    A('Common')->zijin_log($myid,$myusid,"month","会员{$inUserID}的销售奖",$money_count,"agent_use");
                }
            } 
        }
        unset($fee,$s21,$fee_rs,$frs,$where);
    }


    /*
    *合伙人分红
    *
    *$ID            会员ID
    *$inUserID      获奖会员编号
    *$i             合伙人数量
    *$c             合伙人排名
    *$money         注册金额
    *$money_count   最后得到金额
    */
    public function hehuoFH($ID,$inUserID,$money){
        $fck = M('fck');
        $fee = M('fee');
        $fee_rs = $fee->field('s24,s25')->find();
        $data = $fck->where('is_hehuo=1 and is_pay=1')->order('id asc')->select();
        $i = 0;
        foreach ($data as $value) {
            if($value['id'] != $ID){
                $i++;
                if($i >= $fee_rs['s24']){
                    break;
                }
            }   
        }
        $c = 0;
        foreach ($data as $val) {
            if($val['id'] != $ID){

                if($val['can_fenh'] != 0){

                    $money_count = $money*$fee_rs['s25']/(100*$i);

                    if($money_count>0){
                        $this->rw_bonus($val['id'],$val['user_id'],15,$money_count,0,'合伙人分红');
                        A('Common')->zijin_log($val['id'],$val['user_id'],"month","会员{$inUserID}的合伙人分红",$money_count,"agent_use");
                    }
                } 
                $c++;
                if($c >= $fee_rs['s24']){
                    break;
                }
            }
        }
    }


    /*
    *代理分红累计级别
    *
    *$ID            会员ID
    *$s22           累计单数条件
    *$i             累计单数条件数量
    */
    public function dailJB($ID){
        return false;
        $fck = M('fck');
        $fee = M('fee');
        $fee_rs = $fee->field('s22')->find();
        $s22 = explode("|", $fee_rs['s22']);
        $i = 0;
        foreach ($s22 as $value) {
            $i++;
        }
        $where       = array();
        $where['id'] = $ID;
        $field       = 'id,user_id,re_path,father_id,u_level,re_level,is_fx,dl_level';
        $frs         = $this->where($where)->field($field)->find();
        if($frs['re_level'] != 0){
            $lis = member_list($frs['id']);

            foreach ($lis as $key => $val) {

                if($frs['id'] != $val['id']){
                    $wheres = array();
                    $wheres['re_id'] = $val['id'];
                    
                    $wheres['dl_level'] = array('egt',$val['dl_level']);
                    $res = $fck->where($wheres)->count();
                    
                    if($res >= $s22[$val['dl_level']] && $val['dl_level'] < $i){
                        $dl_level = $val['dl_level']+1;
                        $fck->execute("UPDATE __TABLE__ SET dl_level=dl_level+1  WHERE id={$val['id']}"); //加到用户表
                    }

                    if($dl_level == $i){
                        $fck->execute("UPDATE __TABLE__ SET is_dongshi=1  WHERE id={$val['id']}");       //加到用户表
                    }
                }
            }
        }
    }

    /*
    *代理分红分配
    *
    *$ID            会员ID
    *$inUserID      会员编号
    *$s22           累计单数条件
    *$23            分配比例
    *$bl            会员分配的比例
    *money          分配金额
    *$money_count   最后分配金额
    */
    public function dailFH($ID,$inUserID,$repath,$money){
        return false;
        $fee = M('fee');
        $fck = M('fck');
        $fee_rs = $fee->field('s22,s23,s34')->find();
        $s22 = explode("|", $fee_rs['s22']);
        $s23 = explode("|", $fee_rs['s23']);
        $s34 = explode("|", $fee_rs['s34']);
        $field = '*';
        $s = 0;
        foreach ($s23 as $value) {
            $s++;
        }
        $ss = $s-1;
        $lis     = $fck->where('id in (0'.$repath.'0) and dl_level<>'.$ss.' and id<>'.$ID)->field($field)->order('id desc')->select();
        $ms = 0;
        foreach ($lis as $key => $val) {
            $res = $s23[$val['dl_level']-1];
            $money_count = 0;

            if($s23[$val['dl_level']-1] > $ms && $s23[$val['dl_level']-1] != ''){
                $bl = $s23[$val['dl_level']-1] - $ms;
                $money_count = $bl*$money/100;
            }
            if($money_count != 0){
                $this->rw_bonus($val['id'],$inUserID,14,$money_count,0,'代理分红');
                $levels = $s34[$val['dl_level']-1];
                A('Common')->zijin_log($val['id'],$val['user_id'],"month","{$levels}:会员{$inUserID}的代理分红",$money_count,"agent_use");
            }
            $ms = $ms + $bl;

        }

        $num = $fck->where('dl_level={$s} and id <>'.$ID)->count();
        $mrs = $fck->where('dl_level={$s} and id <>'.$ID)->field($field)->select();

        // $num = $fck->where('is_dongshi=1 and id <>'.$ID)->count();
        // $mrs = $fck->where('is_dongshi=1 and id <>'.$ID)->field($field)->select();

        foreach ($mrs as $key => $voo) {

            if($val['is_fx'] != 0){
                $bl = $s23[$s-1];
                $money_count = $bl*$money/(100*$num);
                if($money_count != 0){
                    $this->rw_bonus($voo['id'],$inUserID,14,$money_count,0,'代理分红');
                    $levels = $s34[$val['dl_level']-1];
                    A('Common')->zijin_log($voo['id'],$voo['user_id'],"month","{$levels}:会员{$inUserID}的代理分红",$money_count,"agent_use");
                }
            }
        }
    }


    /*
    *合伙人投资累计级别
    *
    *$ID            会员ID
    *$k             已经被分配的投资分红单位
    *$c             用户可以分配的投资分红单位数量
    */
    public function hehuotzLJ($ID,$inUserID,$money=0){
        return false;
        $fck = M('fck');
        $fee = M('fee');
        $sql = 'where hh_level>0';
        $fee_rs = $fee->field('s26,s27')->find();
        $data = $fck->where('id='.$ID)->field('id,user_id,is_hehuo,hh_level,hh_fenh')->find();
        if($money != 0 && $data['is_hehuo'] == 1){

            $i = $money - $fee_rs['s27'];
            if($i >= 0){
                $res = $fck->query("select id,user_id,sum(hh_level) hh_level from __TABLE__ ".$sql ." order by id asc");
                
                foreach($res as $vo){
                    $k += $vo['hh_level'];        
                }
                
                if($k < $fee_rs['s26']){
                    $c = floor($money/$fee_rs['s27']);
                   
                    if($c + $k >= $fee_rs['s26']){
                        $c = $fee_rs['s26'] - $k;
                    }
                    $data['hh_level'] = $data['hh_level'] + $c;

                    $fck->where('id='.$ID)->data($data)->save(); 
                }
            }
        }
    }



    /*
    *合伙人投资加权分红
    *
    *$ID            会员ID
    *$inUserID      会员编号
    *$c             总分配单位
    *$money_count   会员最后分配到的奖金
    */
    public function hehuotzFH($ID,$inUserID,$money=0){
        $fck = M('fck');
        $fee = M('fee');
        $sql = 'where hh_level>0';
        $fee_rs = $fee->field('s28')->find();
        $res = $fck->query("select id,user_id,sum(hh_level) hh_level from __TABLE__ ".$sql ." order by id desc");

        foreach($res as $vo){
            $c = $vo['hh_level'];     
        }


        $data = $fck->where('hh_level>0')->field('id,user_id,hh_level,can_fenh')->select();

        foreach ($data as $key => $val) {
            if($val['can_fenh'] >0){
                $money_t = $val['hh_level']*$money*$fee_rs['s28']/($c*100);
                $money_count = round($money_t,2);
                if($money_count != 0 && $val['id']!=$ID){

                    $this->rw_bonus($val['id'],$inUserID,16,$money_count,0,'合伙人投资分红');
                    A('Common')->zijin_log($val['id'],$val['user_id'],"month","会员{$inUserID}的合伙人投资分红",$money_count,"agent_use");
                }
            }
        }
    }

    //累计消费
    public function leijiD($uid,$type=0,$money=0){
        $mrs = $this->where('id='.$uid)->find();
        if($type!=0){
            if($mrs){
                $this->hehuotzLJ($mrs['id'],$mrs['user_id'],$money);                    //合伙人投资累计
                $this->y_fenhLLJ($money);                                               //月分红金额累计
                $this->LJ_Spand($mrs['id'],$money);                                     //重复消费累计
            }
        }else{
            if($mrs){
                $this->dailJB($mrs['id']);                                              //代理分红累计级别 
                $this->hehuotzLJ($mrs['id'],$mrs['user_id'],$money);                    //合伙人投资累计
                $this->y_fenhLLJ($money);                                               //月分红金额累计        
                $this->LJ_Spand($mrs['id'],$money);                                     //重复消费累计
                $this->hh_Spand($mrs['id']);                                            //重复消费合伙人累计
            }   
        }
    }

  
    //计算奖金
    public function getusjj($uid,$type=0,$money=0){
        return false;
        $mrs = $this->where('id='.$uid)->find();
        if($type!=0){
            if($mrs){
                // $this->xiaofeijiang($mrs['user_id'],$type,$mrs['re_path'],$mrs['re_level']);
                // $this->lingdaoo($mrs['user_id'],$type,$mrs['p_path']);
                $this->dailFH($mrs['id'],$mrs['user_id'],$mrs['re_path'],$money);       //代理分红
                // $this->hehuoFH($mrs['id'],$mrs['user_id'],$money);                      //合伙人分红
                $this->hehuotzFH($mrs['id'],$mrs['user_id'],$money);                    //合伙人投资分红

            }
            unset($mrs); 
        }else{
            //M('fck')->where('id=1')->setInc('chuju',$mrs['cpzj']); // 开通增加公司总业绩
            $mrs = $this->where('id='.$uid)->find();
            if($mrs){
                // $this->xiaofeijiang($mrs['user_id'],$mrs['cpzj'],$mrs['re_path'],$mrs['re_level']);
                // $this->lingdaoo($mrs['user_id'],$mrs['cpzj'],$mrs['p_path']);
                $this->xiaoshou($mrs['re_id'],$mrs['user_id'],$money);                  //销售奖
                $this->dailFH($mrs['id'],$mrs['user_id'],$mrs['re_path'],$money);       //代理分红
                // $this->hehuoFH($mrs['id'],$mrs['user_id'],$money);                      //合伙人分红
                $this->hehuotzFH($mrs['id'],$mrs['user_id'],$money);                    //合伙人投资分红


            }
            unset($mrs); 
        }
        
    }


    /*
    *合伙人重复消费累计
    *
    *$ID            会员ID
    *$today         今天时间
    *$nextyear      明年时间
    *$res           推荐人
    *$enddays       重复消费后时间
    */
    public function hh_Spand($ID){
        $fck = M('fck');
        $data = $fck->where('id='.$ID)->field('*')->find();
        $fee = M('fee');
        $fee_rs = $fee->field('s33')->find();
        $today = strtotime(date('Y-m-d'))+3600*24-1;        //今天时间
        $nextyear = strtotime(date('Y-m-d',strtotime(" +1 year")))+3600*24-1;        //明年的时间
        if($data['is_hehuo'] == 1){
 
            $fck->execute("UPDATE __TABLE__ SET fenh_end={$nextyear},fenh_start={$nextyear},can_fenh=1  WHERE id={$ID}"); //加到用户表
        }

        $res = $fck->where('id='.$data['re_id'].' and is_hehuo=1')->field('*')->find();
        if($res){
            if($res['fenh_start'] <$today){
                $res['fenh_start'] = $res['fenh_end'];
                $res['re_fenh']   = 0;
            }
            $res['re_fenh'] = $res['re_fenh']+1;
            if($res['re_fenh'] == $fee_rs['s33']){

                $end = date('Y-m-d',$res['fenh_start']);
                // $ends = strtotime($end)+3600*24-1;
                $enddays = date("Y-m-d", strtotime("$end +1 year")); 
                $res['fenh_end'] = strtotime($enddays)+3600*24-1;

            }
            $fck->where('id='.$res['id'])->data($res)->save();
        }

    }    



    /*
    *重复消费累计
    *
    *$ID            会员ID
    *$today         今天时间
    *$enddays       重复消费后结束时间
    *$money_t       会员级别金额
    *$money_count   重复消费级别金额
    *
    */
    public function LJ_Spand($ID,$money=0){
        $fck = M('fck');
        $fee = M('fee');
        $fee_rs = $fee->field('s9,s31,s32')->find();
        $s9 = explode("|", $fee_rs['s9']);
        $s31 = explode("|", $fee_rs['s31']);
        $s32 = explode("|", $fee_rs['s32']);
        foreach ($s9 as $val) {         //会员级别金额
            if($val <= $money){
                $money_t = $val;
            }
        }
        foreach ($s31 as $rs) {        //重复消费级别金额
            if($rs <= $money){
                $money_count = $rs;
            }    
        }

        $today = strtotime(date('Y-m-d'))+3600*24-1;        //今天时间

        $data = $fck->where('id='.$ID)->field('id,is_fx,fx_level,u_level,fx_num,fx_start,fx_end,cpzj')->find();
        if($data['u_level'] == 0 && $money_t != 0){                                     //会员级别改变
            $key = array_search($money_t, $s9); 
            $data['u_level'] = $key+1;
            $data['cpzj'] = $money_t;
        }

        if($data['fx_level'] != 0 && $money_count != 0 && $s31[$data['fx_level']-1] <= $money_count){   //重复消费还是原有级别的情况下
            
            if($data['fx_start'] < $today){
                $data['fx_start'] = $data['fx_end'];
                $data['fx_num']   = 0;
            }

            $day = $s32[$data['fx_level']-1];
            if($data['fx_num'] == 0){

                $end = date('Y-m-d',$data['fx_start']);
                $enddays = date("Y-m-d", strtotime("$end +$day month")); 
                $data['fx_end'] = strtotime($enddays)+3600*24-1;

            }

            $data['fx_num'] = $data['fx_num'] +1;
        }

        if($data['fx_level'] == 0 && $money_count != 0){            //重复消费级别改变
            $keys = array_search($money_count, $s31); 
            $days = $s32[$keys];
            $data['fx_level'] = $keys+1;
            $todays = date('Y-m-d',$today);
            $nextend = date('Y-m-d',strtotime("$todays +$days month"));
            $enddays = strtotime($nextend)+3600*24-1;
            
            $data['fx_start'] = $enddays;
            $data['fx_end']   = $enddays;
            $data['is_fx']    = 1;
        }
        $fck->where('id='.$ID)->data($data)->save();

    }    


    /*
    *重复消费清0
    *
    *$dates         今天时间
    */
    public function Q_Spand(){
        return false;
        $fck = M('fck');

        $dates = strtotime(date('Y-m-d'))+3600*24-1;

        $data = $fck->where('is_pay=1')->field('id,fx_end,fenh_end')->select();

        foreach ($data as $val) {
            //echo $val['id']."====".$val['fx_end']."====".$val['fenh_end']."<br>";
            if($val['fx_end'] < $dates){
               $lis= $fck->execute("UPDATE __TABLE__ SET u_level=0,fx_level=0,is_fx=0,fx_start=0,fx_num=0,fx_end=0,cpzj=0  WHERE id={$val['id']}"); //加到用户表
            }

            if($val['fenh_end'] < $dates){
               $mrs = $fck->execute("UPDATE __TABLE__ SET can_fenh=0,fenh_start=0,re_fenh=0,fenh_end=0 WHERE id={$val['id']}");       //加到用户表 
            }
        }
    }    


    /*
    *月分红金额累计
    *
    *$nowdate
               今天时间
    */
    public function y_fenhLLJ($money=0){
        $times = M('times');
        $nowdate = strtotime(date('Y-m-d'))+3600*24-1;
        $times->execute("UPDATE __TABLE__ SET y_fenh=y_fenh+{$money}  WHERE benqi={$nowdate}"); //加到时间表
    }    


//高管全国分红
    public function dsqgfenh($money=0){
        return false;
        if($money != 0){
            $fck = M('fck');
            $fee = M('fee');
            $fee_rs = $fee->field('s29')->find();
            $data = $fck->where('is_gaoguan=1')->field('id,user_id')->find();
            $money_count = $fee_rs['s29']*$money/100;
            $this->rw_bonus($data['id'],$data['user_id'],17,$money_count,0,'高管全国分红');
            A('Common')->zijin_log($data['id'],$data['user_id'],"month","高管全国分红",$money_count,"agent_use");
        }
    }

//总监全国分红
    public function zjqgfenh($money=0){
        return false;
        if($money != 0){
            $fck = M('fck');
            $fee = M('fee');
            $fee_rs = $fee->field('s30')->find();
            $data = $fck->where('is_zongjian=1')->field('id,user_id')->find();
            $money_count = $fee_rs['s30']*$money/100;
            $this->rw_bonus($data['id'],$data['user_id'],18,$money_count,0,'总监全国分红');
            A('Common')->zijin_log($data['id'],$data['user_id'],"month","总监全国分红",$money_count,"agent_use");
        }
    }


//判断是否进行月分红
    public function y_fenh($bid){
        return false;
        $times = M('times');
        $time = $times->where('id='.$bid)->field('*')->find();
        
        $dates = strtotime(date('Y-m-01'));

        $firstday = date('Y-m-d', $dates);

        $r = $dates - 1;
        $sql = 'where benqi<='.$r.' and type=0';

        $res = $times->query("select id,sum(y_fenh) y_fenh from __TABLE__ ".$sql ." order by id desc");
        foreach($res as $vo){
            $money += $vo['y_fenh'];        
        }
        if($money != 0){

            $rs = $times->where('benqi<='.$r.' and type=0')->field('*')->select();
            foreach ($rs as $val) {

                $val['type'] = 1;
                $times->where('id='.$val['id'])->data($val)->save();
            }
        }
        return $money;
    } 

//董事日分红
    public function quanqiu(){
        $fee_rs = M('fee')->field('s16,s19')->find();
        $s16 = explode('|', $fee_rs['s16']);
        $s19 = explode('|', $fee_rs['s19']);
        $i7 = M('fck')->where('id=1')->find();//公司总业绩
        $i7 = $i7['chuju'];
        $fes = array_combine($s16,$s19);
        $time = strtotime(date('Y-m-d',time()));//时间戳整点
        $yixing = $s16[0]*10000;//一星董事
        $feng = M('fck')->where('zjj>='.$yixing.' and day_feng!='.$time)->order('zjj desc')->select();
        foreach ($feng as $key => $value) {
            foreach ($fes as $key2 => $value2) {
                if($value['zjj']>=$key2*10000){
                    $bai = $value2/100;
                }
            }
            $money = $i7*$bai;
            if($money>0){
            A('Common')->zijin_log($value['id'],$value['user_id'],"month","董事分红",$money,"agent_use");
            }
            M('fck')-> where('id='.$value['id'])->setField('day_feng',$time);//保存分红时间
        }
    
    }

    public function lingdaoo($user_id,$cpzj,$p_path){
        $fee_rs = M('fee')->find();
        $s7 = explode('|', $fee_rs['s7']);//领导津贴
        $s12 = explode('|', $fee_rs['s12']);
        $s5 = $fee_rs['s5'];//最小业绩
        //领导津贴
        $zjj = M('fck')->where('id in(0'.$p_path.'0) and is_pay>0 and is_bb>0')->order('id desc')->select();
        $mus = 0;
        foreach ($zjj as $kk => $vv) {
            $isbb = $vv['is_bb']-1;
            $money = $cpzj*($s7[$isbb]-$mus)/100;
            if($money == 0){
                $money = $cpzj/100;
            }elseif ($money < 0) {
                $money = 0;
            }
            if($money>0){
                
             A('Common')->zijin_log($vv['id'],$vv['user_id'],"month","会员{$user_id}的领导津贴",$money,"agent_use");
             $mus = $isbb+2;
            }
        }

        $zjj = M('fck')->where('id in(0'.$p_path.'0) and is_pay>0')->order('p_level desc')->select();
        //增加总业绩+升级
        foreach ($zjj as $key => $value) {
            if($value['is_bb']<1){
           
                //查询团队业绩
                $l = M('fck')->where('father_id='.$value['id'].' and treeplace=0')->field('cpzj,zjj')->find();//左区
                $l = $l['cpzj']+$l['zjj'];
                $r = M('fck')->where('father_id='.$value['id'].' and treeplace=1')->field('cpzj,zjj')->find();//右区
                $r = $r['cpzj']+$r['zjj'];
                $lr = M('fck')->where('father_id='.$value['id'].' and treeplace=2')->field('cpzj,zjj')->find();//三区
                $lr = $lr['cpzj']+$lr['zjj'];
                $array=array($l,$r,$lr);
                 rsort($array);
                $min = min($array[0],$array[1]);//三区第二小业绩                      
                $yeji = $s12[0]*10000;
                if($value['zjj']>=$yeji && $min>=$s5){
                    $data['is_bb'] = 1;
                    M('fck')->where('id='.$value['id'])->save($data);
                }//领导升级（主任）

             }else{
                 //查询团队业绩
                $l = M('fck')->where('father_id='.$value['id'].' and treeplace=0')->field('id')->find();//左区
                $l = M('fck')->where('p_path like "%'.$l['id'].'%" or id='.$l['id'])->field('is_bb')->order('is_bb desc')->find();//左区团队最小级别

                $r = M('fck')->where('father_id='.$value['id'].' and treeplace=1')->field('id')->find();//右区
                $r = M('fck')->where('p_path like "%'.$r['id'].'%" or id='.$r['id'])->field('is_bb')->order('is_bb desc')->find();
                $lr = M('fck')->where('father_id='.$value['id'].' and treeplace=2')->field('id')->find();//三区
                $lr = M('fck')->where('p_path like "%'.$lr['id'].'%" or id='.$lr['id'])->field('is_bb')->order('is_bb desc')->find();
                $array=array($l,$r,$lr);
                 rsort($array);
                 if($array[1]['is_bb']>=$value['is_bb']){
                    $yeji = $value['is_bb']*10000;
                    if($value['zjj']>=$yeji){
                        $data['is_bb'] = $value['is_bb']+1;
                        if($data['is_bb']>8){
                            $data['is_bb'] = 8;
                        }
                        M('fck')->where('id='.$value['id'])->save($data);
                    }

                 }
             }
             
        }
       

    }

public function aaa(){
    set_time_limit(0);
    //事务开始
    //  在User模型中启动事务
            M()->startTrans();
            // 进行相关的业务逻辑操作
  
  $a = $this->execute("update __TABLE__ set a=a+1 where id=1");
  $b = true;
            if ($a and $b){
                // 提交事务
                M()->commit(); 
            }else{
               // 事务回滚
               M()->rollback(); 
            }
}

   public function xiaofeijiang($user_id,$cpzj,$p_path,$p_level){  
        $fee_rs = M('fee')->find();
        $s6 = explode('|', $fee_rs['s6']);//互助银卡
        $s4 = explode('|', $fee_rs['s4']);//互助金卡
        $s14 = explode('|', $fee_rs['s14']);//互助铂金卡
        $s13 = explode('|', $fee_rs['s13']);//互助钻石卡
        $s15 = explode('|', $fee_rs['s15']);//累计金额

        $p_path = M('fck')->where('id in(0'.$p_path.'0) and is_pay>0')->field('id,re_level,u_level,pdt,user_id')->limit(12)->order('id desc')->select();
        foreach ($p_path as $key => $value) {
            $pleve = $p_level-$value['re_level'];//极差
            $pl = $pleve-1;
            if($value['u_level']>3){
                //钻石卡
                $money = $s13[$pl]/100*$cpzj;
            }elseif($value['u_level']>2){
                $money = $s14[$pl]/100*$cpzj;
                //铂金卡
            }elseif($value['u_level']>1){
                $money = $s4[$pl]/100*$cpzj;
                //金卡
            }else{
                //银卡
                    $money = $s6[$pl]/100*$cpzj;
            }
            if($money>0){
                
            A('Common')->zijin_log($value['id'],$value['user_id'],"month","会员{$user_id}的消费奖励",$money,"agent_use");
            }
            $str1 = explode('|', $fee_rs['str1']);//前面人数
            $s3 = explode('|', $fee_rs['s3']);//后面人数
            //团队互助
//前面人数
             $qian = M('fck')->where('pdt <'.$value['pdt'].' and is_pay>0')->field('id,user_id,u_level')->limit(20)->order('id desc')->select();      
             $pai = 0;
             foreach ($qian as $key2 => $value2) {
                 $pai = $pai+1;//相差人数
                 $ule = $value2['u_level']-1;//人数键值
                    if($pai <= $str1[$ule]){
                        $money_count = $money*0.005;
                        if($money_count>0){
                                    A('Common')->zijin_log($value2['id'],$value2['user_id'],"month","会员{$value['user_id']}的团队互助奖励",$money_count,"agent_use");
                                        //自助升级
                                        $this->jijin($value2['id'],$money_count);
                        }
                    }

                 //归零
                 $money_count = 0;
             }


//后面人数
             $hou = M('fck')->where('pdt >'.$value['pdt'].' and is_pay>0')->field('id,user_id,u_level')->limit(20)->order('id')->select();
             $pai = 0;
             foreach ($hou as $key3 => $value3) {
                  $pai = $pai+1;//相差人数
                 $ule = $value3['u_level']-1;//人数键值
                 if($pai <= $s3[$ule]){
                    $money_counts = $money*0.005;
                    if($money_counts>0){
                        A('Common')->zijin_log($value3['id'],$value3['user_id'],"month","会员{$value['user_id']}的团队互助奖励",$money_counts,"agent_use");
                        $this->jijin($value3['id'],$money_counts);//累计升级
                    }
                 }

                 $money_counts = 0;
             }

              $money = 0;

        }
   }
    
    public function jijin($id,$money_count){
        $fee_rs = M('fee')->find();
        $s15 = explode('|', $fee_rs['s15']);//累计金额
        $value2 = M('fck')->where('id='.$id)->find();

                                        // if($value2['p_level']>=2){     
                                            $cen = $value2['p_level'];
                                            if($value2['leiji']+$money_count>=$s15[$value2['benqi_l']]){
                                                $ji = $value2['benqi_l']+1;
                                                   //算钱
                                                    $money_count2 = $s15[$value2['benqi_l']]*0.5;
                                                    if($money_count2>0){
                                                        //节点人
                                                        $re = $this->where('id='.$value2['father_id'].' and benqi_l>='.$ji)->field('id,user_id')->find();
                                                        A('Common')->zijin_log($re['id'],$re['user_id'],"month","升级基金奖励",$money_count2,"agent_use"); 

                                                        $da['leiji'] = $value2['leiji']+$money_count-$s15[$value2['benqi_l']];//累计金额
                                                        if($ji>18){
                                                            $da['benqi_l'] = 0;
                                                        }else{
                                                            $da['benqi_l'] = $ji;
                                                        }
                                                        M('fck')->where('id='.$value2['id'])->save($da);
                                                    }                                                     
                                            }else{
                                                $da['leiji'] = $value2['leiji']+$money_count;//累计金额
                                                M('fck')->where('id='.$value2['id'])->save($da);
                                            }
                                              
                                        // }
    }
   
    //各种扣税
    public function rw_bonus($myid,$inUserID=0,$bnum=0,$money_count=0,$money_type,$bz){

        $fee = M('fee');
        $fee_rs = $fee->field('s9,str4,str5,str9,s6,i17,i18,i21,s4,i29')->find();

        // $s4 = explode('|',$fee_rs['s4']);

        //动态积分: 每次所产生动态奖金的10%到保管金，累积到100美金可以转换为报单币。
        //money_type=1
        // $agent_bg = 0;
        $usqla = "";
        //动态积分: 每次所产生动态奖金的10%到保管金，累积到100美金可以转换为报单币。
        // if ($money_type == 1){
        //     $agent_bg =  $s4[0]/100 * $money_count;
        //     $usqla = ",agent_bg=agent_bg+".$agent_bg."";
        // }

        //静态收入扣10%做为消费积分（用来转出买商城的产品 名称：SDK商城）
        // if ($money_type == 2){
        //     $agent_bg =  $fee_rs['i29'] /100* $money_count;
        //     $usqla = ",agent_cf=agent_cf+".$agent_bg."";
        // }

        $last_m = $money_count - $agent_bg;//剩余，此值写入现金账户
        $bonus = M('bonus');
        $bid = $this->_getTimeTableList($myid);
        $inbb = "b".$bnum;
        $usqlc = "agent_use";//agent_use


        $bonus->execute("UPDATE __TABLE__ SET b0=b0+".$last_m.",".$inbb."=".$inbb."+".$money_count."  WHERE id={$bid}"); //加到记录表
        //$this->execute("update __TABLE__ set ".$usqlc."=".$usqlc."+".$last_m.",zjj=zjj+".$money_count.",xy_money=xy_money+".$money_count."".$usqla." where id=".$myid);//加到fck
        // echo $this->getLastSql();exit;

        unset($bonus);

        if($money_count>0){
            $this->addencAdd($myid,$inUserID,$money_count,$bnum,0,0,0,$bz,0,$times);
        }

        //if($money_ka>0){
//          $this->addencAdd($myid,$inUserID,-$money_ka,6);
//      }

//      if($money_kc>0){
//          $this->addencAdd($myid,$inUserID_did,-$money_kb,8);
//      }
//
//      if($last_m>0){
//          $this->addencAdd($myid,$inUserID,$last_m,25,0,0,0,"进入奖金账户");
//      }

        unset($fee,$fee_rs,$mrs);
    }


    public  function _getTimeTableList($uid,$type=0)
    {
        $times = M ('times');
        $bonus = M ('bonus');
        $boid = 0;
        $nowdate = strtotime(date('Y-m-d'))+3600*24-1;
        $settime_two['benqi'] = $nowdate;
        $settime_two['type']  = 0;
        $trs = $times->where($settime_two)->find();
        if (!$trs){
            $rs3 = $times->where('type=0')->order('id desc')->find();
            if ($rs3){
                $data['shangqi']  = $rs3['benqi'];
                $data['benqi']    = $nowdate;
                $data['is_count'] = 0;
                $data['type']     = 0;
            }else{
                $data['shangqi']  = strtotime('2010-01-01');
                $data['benqi']    = $nowdate;
                $data['is_count'] = 0;
                $data['type']     = 0;
            }
            $shangqi = $data['shangqi'];
            $benqi   = $data['benqi'];
            unset($rs3);
            $boid = $times->add($data);
            unset($data);
        }else{
            $shangqi = $trs['shangqi'];
            $benqi   = $trs['benqi'];
            $boid = $trs['id'];
        }
        $_SESSION['BONUSDID'] = $boid;
        $brs = $bonus->where("uid={$uid} AND did={$boid}")->find();
        if ($brs){
            $bid = $brs['id'];
        }else{
            $frs = $this->where("id={$uid}")->field('id,user_id')->find();
            $data = array();
            $data['did'] = $boid;
            $data['uid'] = $frs['id'];
            $data['user_id'] = $frs['user_id'];
            $data['e_date'] = $benqi;
            $data['s_date'] = $shangqi;
            $bid = $bonus->add($data);
        }
        if($type != 0){
            $bid = $boid;
        }
        return $bid;
    }
}
?>